package cn.lut.mvc.interceptor;

import cn.lut.constant.CrowdConstant;
import cn.lut.entity.Admin;
import cn.lut.exception.AccessForbiddenException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @user:dell
 * @date:2020年11月23 14:07
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
//       1.通过request对象获取session对象
        HttpSession session = httpServletRequest.getSession();
//        2.尝试从Session域中获取admin对象
        Admin admin =(Admin) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN);
//        3.判断admin对象是否为空
        if(admin==null){
//            4.抛出异常
            throw new AccessForbiddenException(CrowdConstant.MESSAGE_ACCESS_FORBIDDEN);
        }
//5.若果对象不为null，则返回true，放行
        return true;
    }
}